<?php
include  'loginCheck.php';
include  'Util.php';
include_once 'generateRSS.php';

if (isset($_POST['delete'])) {
    $link = Util::getLink();
    for ($i = 1; $i < $_POST['count']+1; $i++) {
        if (isset($_POST['element_1_'.$i])) {
            $query = sprintf(Util::$delete_jornakDestination,
                mysql_real_escape_string($_POST['element_1_'.$i])
            );
            $result = mysql_query($query);

            // Check result
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
        //echo ("Query succesful. <br>");
        }
    }
    Util::closeLink($link);

    header("location: ".Constants::$link_done_fromResolve);
    exit();
}

if (isset($_POST['edit'])) {

    if (isset($_POST['element_1'])) {
        header("location: ".Constants::$link_editNews.$_POST['element_1']);
        exit();
    //echo ("Query succesful. <br>");
    }
}

if (isset($_POST['final'])) {
    $link = Util::getLink();
    if (isset($_POST['submit']) && $_POST['submit'] == "Back" ) {
        /*$query = sprintf(Util::$delete_jornakDestinationByTextId,
            mysql_real_escape_string($_POST['oldTextId'])
        );
        $result = mysql_query($query);

        // Check result
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }*/

        $query = sprintf(Util::$select_jornakDestinationAllById,
            mysql_real_escape_string($_POST['final'])
        );
        $result = mysql_query($query);

        // Check result
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }

        $row = mysql_fetch_assoc($result);

        Util::closeLink($link);
        header("location: ".Constants::$link_editNews.$row['textid']);
        exit();
    }else {
        if (isset($_POST['finalEdit']) && isset($_POST['oldTextId'])) {
            $query = sprintf(Util::$delete_jornakDestinationByTextId,
                mysql_real_escape_string($_POST['oldTextId'])
            );
            $result = mysql_query($query);

            // Check result
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
        }

        // Zapis do destination
        $query = sprintf(Util::$edit_jornakDestinationChangeToNotPreview,
            mysql_real_escape_string($_POST['final'])
        );

        // Perform Query
        $result = mysql_query($query);

        // Check result
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }
        
        fibixCreateRSS();
        
//        // nacteni mailu
//        $query = sprintf(Util::$select_jornakDestinationAllById,
//            mysql_real_escape_string($_POST['final'])
//        );        
//        // Perform Query
//        $result = mysql_query($query);
//
//        // Check result
//        if (!$result) {
//            $message  = 'Invalid query: ' . mysql_error() . "\n";
//            $message .= 'Whole query: ' . $query;
//            die($message);
//        }
//        
//        $row = mysql_fetch_assoc($result);
//        
//        //nacteni textu mailu
//        $query = sprintf(Util::$select_jornakText,
//            mysql_real_escape_string($row['textid'])
//        );
//        mysql_free_result($result);
//        // Perform Query
//        $result = mysql_query($query);
//
//        // Check result
//        if (!$result) {
//            $message  = 'Invalid query: ' . mysql_error() . "\n";
//            $message .= 'Whole query: ' . $query;
//            die($message);
//        }
//        
//        $row = mysql_fetch_assoc($result);
//
//        

        //Util::sendMail("News From Fibix - " . $row['header'], $row['text']);

        Util::closeLink($link);
        header("location: ".Constants::$link_done_fromResolve);
        exit();
    }
}

if (isset($_POST['saveNew']) || isset($_POST['saveEdit'])) {
    $link = Util::getLink();
    // zapis textu
    $query = sprintf(Util::$insert_jornakText,
        mysql_real_escape_string($_POST['header']),
        mysql_real_escape_string($_POST['text']),
        mysql_real_escape_string($_POST['list']),
        mysql_real_escape_string($_POST['video']),
        mysql_real_escape_string($_POST['imgs']),
        mysql_real_escape_string($_POST['rssDesc'])
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }


    // Zapis do destination
    $query = sprintf(Util::$insert_jornakDestinationPreview,
        mysql_real_escape_string('1'),
        mysql_real_escape_string(mysql_insert_id()),
        //2011-04-11 16:01:31
        mysql_real_escape_string($_POST['dateY'].'-'.$_POST['dateM'].'-'.$_POST['dateD'].' '.date('h:i:s'))
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }

    $finalString = Constants::$link_previewNews;
    if (isset($_POST['oldTextId'])) {
        $finalString = $finalString.'&oldTextId='.$_POST['oldTextId'];
    }
    $finalString = str_replace('$a',mysql_insert_id(),$finalString);

    if (isset($_POST['saveNew'])) {
        $finalString = str_replace('$b',"1",$finalString);
    }else {
        $finalString = str_replace('$b',"0",$finalString);
    }
    Util::closeLink($link);
    header("location: ".$finalString);
    exit();
}

?>
